package com.kqkj.dyrule.engine.queryService;

import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Get;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.Map;
import java.util.Set;

public class HbaseQueryServiceImpl implements QueryService{

    Connection hbaseConnection;
    public HbaseQueryServiceImpl(Connection hbaseConn){
        this.hbaseConnection = hbaseConn;
    }

    public boolean queryProfileConnection(String deviceId, Map<String,String> userProfileConditions) throws IOException {

        Set<String> tags = userProfileConditions.keySet();
        Table table = hbaseConnection.getTable(TableName.valueOf("zenniu_profile"));
        Get get = new Get(Bytes.toBytes(deviceId));
        for (String tag : tags) {
            get.addColumn(Bytes.toBytes("f"),Bytes.toBytes(tag));
        }
        Result result = table.get(get);
        for (String tag : tags) {
            byte[] value = result.getValue(Bytes.toBytes("f"), Bytes.toBytes(tag));
            if (!userProfileConditions.get(tag).equals(new String(value))) return false;
        }
        return true;


    }
}
